package com.eco.fanliapp.job.orderjobNew;


import com.eco.fanliapp.common.OrderTaskUtils;
import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IJobOrderInfoHandleNewService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;

/**
 * 拉取付款订单
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class OrderPayTaskNew implements Job, Serializable {

    private static final Logger log = LoggerFactory.getLogger(OrderPayTaskNew.class);

    @Autowired
    private IJobOrderInfoHandleNewService jobOrderInfoHandleNewService;

    @Autowired
    private OrderTaskUtils orderTaskUtils;


    public OrderPayTaskNew() {}

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long beginTime = System.currentTimeMillis();
        log.info("***************************************开始执行定时任务-拉取付款订单定时任务-OrderPayTaskNew：executeCount=" + executeCount + "--beginTime=" + beginTime);

        //开始时间、结束时间
        String startTime = OrderTaskUtils.getSystemTime();
        String endTime = OrderTaskUtils.getNowTime();

        //获取淘宝联盟账号-排除已过期的
        List<TaobaoScheme> taobaoSchemes = orderTaskUtils.getUsedTaobaoSchemeList();

        //处理订单
        jobOrderInfoHandleNewService.handlePayOrder(startTime, endTime, taobaoSchemes, 0);

        log.info("***************************************结束执行定时任务-拉取付款订单定时任务-OrderPayTaskNew：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (System.currentTimeMillis() - beginTime));

    }


}
